$(document).ready(function(){    
        
    $('#cancel_button').click(function(){
        window.location = getBaseURL();
    });
    
    $('#submit_button').click(function(){
        if (validateData()){
            postChangePassword();
        }
    });
});

function validateData(){
    var responses = new Object();
    var retval = true;
    if ($("#input_old").val() == ""){
        responses["#old_section"] = "Field is empty";
        retval = false;
    }
    if ($("#input_new").val() != $("#input_new_confirm").val()){
        responses["#new_section"] = "Passwords do not match";
        retval = false;
    }
    if ($("#input_new").val() == ""){
        responses["#new_section"] = "Fields are empty";
        retval = false;
    }
    highlightSections(responses);
    
    return retval;
}

function postChangePassword(){
    var oldpass = $("#input_old").val();
    var newpass = $("#input_new").val();
    
    $.post(getBaseURL()+"settings/change_password/save/",{
        oldpass: oldpass,
        newpass: newpass
    },
    function(data){
        if (data == "good"){
            window.location = getBaseURL();
        }
        else{
            location.reload();
        }
    });
}